package com.eslserver.action.image;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.LoggerFactory;

import com.eslserver.core.aware.ESLAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLDAO;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.to.ESL;
import com.eslserver.to.Image;
import com.eslserver.to.wrapper.ImageWrapper;
import com.eslserver.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadImageServlet extends HttpServlet implements ESLAware {
	public static final org.slf4j.Logger LOG = LoggerFactory
			.getLogger(LoadImageServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		Writer writer = resp.getWriter();
		try {
			String eslID = req.getParameter(PARAM_ESL_ID);

			ESLDAO eslDAO = ESLServerDAOFactory.getDAOFactory(
					ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getESLDAO();
			ESL esl = eslDAO.loadESL(Integer.valueOf(eslID));
			Image eslImage = esl.getImages().size() > 0 ? esl.getImages()
					.iterator().next() : null;

			ImageWrapper imageWrapper = ImageWrapper
					.createESLImageWrapper(eslImage);

			writer.write(JSONResponseUtil.getJSONObjectString(imageWrapper));
		} catch (DAOException e) {
			LOG.error("error laoding Talent", e);
		} finally {
			writer.close();
		}
	}
}
